package com.example.mallsystem1104.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mallsystem1104.mapper.IncomeMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author zy
 * @Date 2024/12/13 17:38
 * @packageName:com.example.mallsystem1104.service
 * @ClassName:IncomeService
 */
@Service
public class IncomeService {
    @Resource
    private IncomeMapper incomeMapper;

    public Map<String,Object> getChart() {
        Map<String,Object> chartMap=new HashMap<>();
        //查询每个分类及其收入
        List<Map<String,Object>> categoryIncomes=incomeMapper.selectCategoryIncome();
        //查询所有收入
        BigDecimal sumIncome=incomeMapper.selectSumIncome();

        chartMap.put("categoryIncomes",categoryIncomes);
        chartMap.put("sumIncome",sumIncome);
        return chartMap;
    }

    public Object getWeekIncome() {
        // 初始化一个列表用于存储每天的收入
        ArrayList<BigDecimal> weekIncome=new ArrayList<>();
        // 获取本周日期
        ArrayList<String> weekdays=new ArrayList<>();
        // 确定本周开始的日期时间
        DateTime dateTime= DateUtil.beginOfWeek(DateUtil.date());
        // 遍历本周的每一天
        for (int i=0;i<7;i++){
            // 往后推几天
            DateTime thisday=DateUtil.offsetDay(dateTime,i);
            DateTime nextDay=DateUtil.offsetDay(dateTime,i+1);
            // 获取当前天的日期字符串
            String weekDay=thisday.toString();
            // 格式化日期字符串，仅保留月-日的部分
            String formattedWeekday=weekDay.substring(weekDay.indexOf("-")+1,weekDay.indexOf(" "));
            weekdays.add(formattedWeekday);
            // 从数据源获取当天的收入
            BigDecimal dayIncome=incomeMapper.getDayIncome(thisday.toString(),nextDay.toString());
            // 如果当天没有收入记录，则将收入设置为0
            if (dayIncome==null){
                dayIncome=new BigDecimal(0);
            }
            // 将当天的收入添加到周收入列表中
            weekIncome.add(dayIncome);
        }
        // 创建一个Map对象用于存储周日期和周收入信息
        HashMap<String,Object> map =new HashMap<>();
        // 将周日期和周收入信息放入Map中
        map.put("weekDays",weekdays);
        map.put("weekIncome",weekIncome);
        // 返回包含周收入信息的Map对象
        return map;
    }

    public Object getMonthIncome() {
        ArrayList<BigDecimal> monthIncome=new ArrayList<>();
        ArrayList<String> monthdays=new ArrayList<>();
        DateTime dateTime= DateUtil.beginOfMonth(DateUtil.date());
        for (int i=0;i<30;i++){
            //往后推几天
            DateTime thisday=DateUtil.offsetDay(dateTime,i);
            DateTime nextDay=DateUtil.offsetDay(dateTime,i+1);
            String weekDay=thisday.toString();
            String formattedWeekday=weekDay.substring(weekDay.indexOf("-")+1,weekDay.indexOf(" "));
            monthdays.add(formattedWeekday);
            BigDecimal dayIncome=incomeMapper.getDayIncome(thisday.toString(),nextDay.toString());
            if (dayIncome==null){
                dayIncome=new BigDecimal(0);
            }
            monthIncome.add(dayIncome);
        }
        HashMap<String,Object> map =new HashMap<>();
        map.put("monthDays",monthdays);
        map.put("monthIncome",monthIncome);
        return map;
    }
}
